// pages/task_queue/task_queue.js
var app = getApp()
var page = 0;
var page_size = 5
var header = {};
var timer = null;
// var refreshTask = function (that) {
//   that.setData({
//     hidden: false
//   });
//   wx.request({
//     url: 'https://119.3.46.145/server/FindMissionNoSuccess?userid=' + app.globalData.userid + '&start=' + page_size * page + '&num=' + page_size,
//     method: 'post',
//     header: header,
//     success: function (res) {
//       let data = JSON.parse(res.data.data);
//       console.log(data, "data");
//       if (data.length == 0) {
//         that.setData({
//           hidden: true
//         });

//         return;
//       }
//       var tmp_list = that.data.list;
//       for (var i = 0; i < data.length; i++) {
//         tmp_list.push(data[i]);
//       }
//       that.setData({
//         list: tmp_list
//       });
//       page++;
//       that.setData({
//         hidden: true
//       });
//     }
//   });
// }
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hidden: true,
    list: [],
    scrollTop: 0,
    scrollHeight: 0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    header = {
      'content-type': 'application/x-www-form-urlencoded',
      'cookie': "openid=" + app.globalData.openid
    }
    
    // wx.getSystemInfo({
    //   success: function (res) {
    //     console.info(res.windowHeight);
    //     that.setData({
    //       scrollHeight: res.windowHeight
    //     });
    //   }
    // });

    that.refreshTask();
  
    timer = setInterval(function () {
      console.log("task_queue timer");
      that.refreshTask();
    }, 10000);
  },

  refreshTask:function()
  {
    let that = this;
    console.log("setINterval");
    wx.request({
      url: "https://119.3.46.145/server/FindMissionNoSuccess",
      method: "post",
      header:header,
      data: {
        userid: app.globalData.userid
      },
      success: res=>{
        //[{result: "等待设备上线", name: "测试区域2-测试路段-灯0329", id: "59", time: "2020-01-03 15:08:04.0", type: "合上开关"},
        //{result: "等待设备上线", name: "测试区域2-测试路段-test2659", id: "60", time: "2020-01-03 15:08:04.0", type: "合上开关"},
        //{result: "等待设备上线", name: "测试区域2-测试路段-test444", id: "61", time: "2020-01-03 15:08:04.0", type: "合上开关"},
        //{result: "等待设备上线", name: "测试区域2-测试路段-test4", id: "62", time: "2020-01-03 15:08:04.0", type: "合上开关"},
        //{result: "等待设备上线", name: "测试区域2-测试路段-test4=5", id: "63", time: "2020-01-03 15:08:04.0", type: "合上开关"},
        //{result: "等待设备上线", name: "测试区域2-测试路段-test555", id: "64", time: "2020-01-03 15:08:04.0", type: "合上开关"},
        //{result: "等待设备上线", name: "测试区域2-测试路段-test666", id: "65", time: "2020-01-03 15:08:04.0", type: "合上开关"}] 
        let array = JSON.parse(res.data.data);
        that.setData({
          list:array
        });    
      }
    })
  },

  cancelMisson:function(e){
    let that  = this;

    //console.log(e, "e");
    let id = e.currentTarget.dataset.id;
    
    wx.request({
      url: "https://119.3.46.145/server/CancelMission",
      method: "post",
      header:header,
      data: {
        id: id
      },
      success: function () {
        let tmp_list = that.data.list;
        try {
          tmp_list.forEach(function (item, index, self) {
            if (item.id == id) { 
              self.splice(index, 1);          
              throw new Error("ending");//报错，就跳出循环
            }           
          })
        } catch (e) {
          if (e.message == "ending") {
            // log("结束了");
          }
        }
      
        that.setData({
          list:tmp_list
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {  
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    console.log("onUnload")
    let that = this;
    if(timer!=null){
      clearInterval(timer);
    }    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  load: function (s) {
    wx.showLoading({
      title: s,
    });
  },
  disLoad: function () {
    wx.hideLoading();
  }
})